const express = require('express')

const router = express.Router()
const db = require('../../config/db.js')
// 引入jwt token工具
const JwtUtil = require('../../utils/jwt');

const dayjs = require('dayjs');

//获取所有角色信息
router.get('/roleinfor', function (req, res) {
    // 前端接收参数
    let role = req.query.role;
    let searchKey = req.query.searchKey;
    let option = req.query.option;
    // 请求需要进行token校验 
    console.log("role", role);
    console.log("searchKey", searchKey);
    console.log("option", option);
    // console.log(req.query);
    // let token = req.headers.etoken;
    // let jwt = new JwtUtil(token)
    // let result = jwt.verifyToken()
    // console.log(result.data);
    // let id = result.data;
    let user_check_sql;
    if (role && !searchKey) {
        user_check_sql = 'select * from user where role="' + role + '"';
    } else if (role && searchKey) {
        user_check_sql = 'select * from user where role="' + role + '" and jobnumber="' + searchKey + '" or userName="' + searchKey + '"';
    }
    // 查询用户表
    // let user_check_sql = 'select * from user where role="' + role + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            let result = option ? rows.map((item) => {
                return {
                    userName: item.userName,
                    jobnumber: item.jobnumber
                }
            }) : rows.map((item) => {
                return {
                    phone: item.phone,
                    userName: item.userName,
                    role: item.role,
                    avatar: item.avatar,
                    gender: item.gender,
                    jobnumber: item.jobnumber
                }
            });
            res.send({
                data: {
                    code: 200,
                    msg: '查询成功',
                    success: true,
                    data: {
                        result
                    }
                }
            })
        }
    })
});

// 删除数据
router.delete('/delete', function (req, res) {
    console.log(req.query);
    let jobnumber = req.query.jobnumber;
    let id = req.query.id;
    let classifyId = req.query.classifyId;
    // console.log(jobnumber);
    // "delete from  数据表 where 字段 = '" + 值 + "'"
    let sql;
    if (id) {
        sql = 'delete from Departmentinfo where id = "' + id + '"';
    }
     else if (jobnumber) {
        sql = 'delete from User where jobnumber = "' + jobnumber + '"';
    }
     else if (classifyId) {
        sql = 'delete from Departmentclassify where id = "' + classifyId + '"';
    }
    // let sql = 'delete from User where jobnumber = "' + jobnumber + '"';
    db.query(sql, function (err) {
        if (err) {
            res.send({
                code: -1,
                msg: '删除失败',
                err,
            })
        } else {
            res.send({
                data: {
                    code: 200,
                    msg: '删除成功',
                    success: true,
                }
            })
        }
    })

})

// 增加科室数据
router.post('/addDepartment', async function (req, res) {
    console.log(req.body);
    let name = req.body.name; // 科室名
    let id = dayjs().valueOf();
    // 查询用户表
    let user_check_sql = 'select * from department where name = "' + name + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.name == name) {
                    res.send({
                        code: 201,
                        msg: '科室已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Department(name,id) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功',
                                success: true
                            }
                        });
                    }
                })
            }

        }
    })
})
// 增加科室分类
router.post('/addclassify', async function (req, res) {
    console.log(req.body);
    let name = req.body.name; // 科室名
    let classify = req.body.classify; // 科室类别
    let id = dayjs().valueOf();
    // 查询用户表
    let user_check_sql = 'select * from departmentclassify where classify = "' + classify + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.name == name) {
                    res.send({
                        code: 201,
                        msg: '科室类别已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Departmentclassify(name,id,classify) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ',' + '"' + classify + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功',
                                success: true
                            }
                        });
                    }
                })
            }

        }
    })
})

// 增加科室信息
router.post('/departmentInfo', async function (req, res) {
    console.log(req.body);
    let name = req.body.name; // 科室名
    let classify = req.body.classify; // 科室类别
    let id = req.body.id;
    let jobnumber = req.body.jobnumber;
    let userName = req.body.userName;
    let img = req.body.img;
    let address = req.body.address;
    // 查询用户表
    let user_check_sql = 'select * from departmentinfo where jobnumber = "' + jobnumber + '" and id = "' + id + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.name == name) {
                    res.send({
                        code: 201,
                        msg: '科室信息已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Departmentinfo(name,id,classify,jobnumber,userName,img,address) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ',' + '"' + classify + '"' + ',' + '"' + jobnumber + '"' + ',' + '"' + userName + '"' + ',' + '"' + img + '"' + ',' + '"' + address + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功',
                                success: true
                            }
                        });
                    }
                })
            }

        }
    })
})

//获取所有科室名称/分类
router.get('/department', function (req, res) {
    // 前端接收参数
    let key = req.query.key;
    let searchKey = req.query.searchKey;
    // 请求需要进行token校验 
    console.log("key", key);
    console.log("searchKey", searchKey);
    let user_check_sql;
    if (key == 'department') {
        user_check_sql = 'select * from department';
    } else if (key == 'classify' && searchKey) {
        user_check_sql = 'select * from departmentclassify where classify = "' + searchKey + '" or name = "' + searchKey + '"';
    } else if (key == 'classify') {
        user_check_sql = 'select * from departmentclassify';
    }
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows
                    },
                    result: rows
                },
                code: 200,
                msg: '获取成功',
                success: true,
            })
        }
    })
});

//获取所有科室信息
router.get('/departmentInfo', function (req, res) {
    // 前端接收参数
    let searchKey = req.query.searchKey;
    let classify = req.query.classify;
    let jobnumber = req.query.jobnumber;
    console.log("searchKey", searchKey);

    let user_check_sql;
    if (searchKey) {
        user_check_sql = 'select * from departmentinfo where jobnumber = "' + searchKey + '" or id = "' + searchKey + '" or name = "' + searchKey + '" or userName = "' + searchKey + '" or classify = "' + searchKey + '"';
    } else if (jobnumber && classify) {
        user_check_sql = 'select * from departmentinfo where jobnumber = "' + jobnumber + '" and classify = "' + classify + '"';
    } else {
        user_check_sql = 'select * from departmentinfo';
    }
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows
                    },
                    result: rows
                },
                code: 200,
                msg: '获取成功',
                success: true,
            })
        }
    })
});

module.exports = router;